﻿using System;
using System.Linq;
using Raven.Client;
using Sponsorship.Domain;

namespace Sponsorship.ReadModel
{
    public static class IDocumentSessionExtension
    {
        public static T LoadById<T>(this IDocumentSession session, Guid childId) where T : Dto
        {
            return session.Load<T>(Dto.GetDtoIdOf<T>(childId));
        }

        public static void SaveChanges(this IDocumentSession session, Dto dto, IEvent domainEvent) 
        {
            // Getting the event version out of the event headers
            dto.Version = domainEvent.GetVersion();

            session.SaveChanges();
        }
    }
}
